//
//  ZLScrollViewController.m
//  MapleKit_Example
//
//  Created by 赵磊 on 2020/2/15.
//  Copyright © 2020 itzhaolei. All rights reserved.
//

#import "ZLScrollViewController.h"
#import "ZLScrollToolBar.h"

@interface ZLScrollViewControllerScroll : UIScrollView

///内容视图
@property (nonatomic,weak) UIView *contentView;

@end

@implementation ZLScrollViewControllerScroll

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.alwaysBounceVertical = YES;
        self.contentSize = self.bounds.size;
        self.backgroundColor = UIColor.whiteColor;
        //ios11 适配
        if (@available(iOS 11.0, *)) {
            self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            self.scrollIndicatorInsets = self.contentInset;
        }
    }
    return self;
}

#pragma mark - Set
- (void)setContentSize:(CGSize)contentSize {
    super.contentSize = contentSize;
    self.contentView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height);
}

#pragma mark - Lazy
- (UIView *)contentView {
    if (!_contentView) {
        UIView *view = [[UIView alloc] initWithFrame:self.bounds];
        view.backgroundColor = UIColor.whiteColor;
        [self addSubview:view];
        _contentView = view;
    }
    return _contentView;
}

@end

@interface ZLScrollViewController ()<UIScrollViewDelegate>

///滑动视图
@property (nonatomic,weak) ZLScrollViewControllerScroll *scrollView;
///滑动内容视图
@property (nonatomic,weak) UIView *contentView;
///滚动
@property (nonatomic,unsafe_unretained) BOOL isScroll;
///点击
@property (nonatomic,unsafe_unretained) BOOL isClick;
///选中的索引
@property (nonatomic,unsafe_unretained) NSInteger selectIndex;
///工具条
@property (nonatomic,weak) ZLScrollToolBar *toolsBar;

@end

@implementation ZLScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - Set
- (void)setTitles:(NSArray<NSString *> *)titles {
    _titles = titles;
    self.toolsBar.titles = titles;
}
- (void)setActionIndex:(NSInteger)actionIndex {
    self.toolsBar.selectIndex = actionIndex;
}
- (void)setViewControllers:(NSArray<NSString *> *)viewControllers {
    _viewControllers = viewControllers;
    for (NSString *vcName in viewControllers) {
        UIViewController *vc = [[NSClassFromString(vcName) alloc] init];
        NSInteger index = [viewControllers indexOfObject:vcName];
        vc.view.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width * index, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
        [self.scrollView.contentView addSubview:vc.view];
        [self addChildViewController:vc];
        self.scrollView.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width * viewControllers.count, UIScreen.mainScreen.bounds.size.height);
    }
    [self.view bringSubviewToFront:self.toolsBar];
}

#pragma mark - Lazy
- (ZLScrollToolBar *)toolsBar {
    if (!_toolsBar) {
        BOOL isBangDevice = NO;
        if (@available(iOS 11.0, *)) {
            isBangDevice = UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
        }
        CGFloat navigationHeight = isBangDevice ? 84.0 : 64.0;
        ZLScrollToolBar *view = [[ZLScrollToolBar alloc] initWithFrame:CGRectMake(0, navigationHeight, UIScreen.mainScreen.bounds.size.width, 45.0)];
        view.backgroundColor = UIColor.whiteColor;
        
        __weak typeof(self)weakSelf = self;
        view.selected = ^(NSInteger index) {
            if (!weakSelf.isScroll) {
                weakSelf.isClick = YES;
                [weakSelf.scrollView setContentOffset:CGPointMake(UIScreen.mainScreen.bounds.size.width * index, 0) animated:NO];
                [weakSelf scrollViewDidEndDecelerating:weakSelf.scrollView];
                weakSelf.selectIndex = index;
                if (weakSelf.reloadData) {
                    weakSelf.reloadData(index);
                }
                return ;
            }
            weakSelf.isScroll = NO;
            weakSelf.selectIndex = index;
            if (weakSelf.reloadData) {
                weakSelf.reloadData(index);
            }
        };
        
        [self.view addSubview:view];
        _toolsBar = view;
    }
    return _toolsBar;
}
- (ZLScrollViewControllerScroll *)scrollView {
    if (!_scrollView) {
        ZLScrollViewControllerScroll *scrollView = [[ZLScrollViewControllerScroll alloc] initWithFrame:UIScreen.mainScreen.bounds];
        scrollView.delegate = self;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        //ios11 适配
        if (@available(iOS 11.0, *)) {
            scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            scrollView.scrollIndicatorInsets = scrollView.contentInset;
        }
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:scrollView];
        _scrollView = scrollView;
    }
    return _scrollView;
}
- (UIView *)toolBar {
    return self.toolsBar;
}
- (void)resetConfig:(ZLScrollToolBarConfig *)config {
    self.toolsBar.config = config;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.isClick) {
        self.isClick = NO;
        return;
    }
    self.isScroll = YES;
    NSInteger index = scrollView.contentOffset.x / UIScreen.mainScreen.bounds.size.width;
    if (self.selectIndex == index) {
        self.isScroll = NO;
        return;
    }
    self.toolsBar.selectIndex = index;
}

@end
